﻿
using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.WebApp;
using Gift.Model;
using Gift.Utile;
using System.IO;
using Gift.DAL;

namespace WebApp.admin.card
{
    public partial class cardType : BaseAdmin
    {

    
        tbCardType oCardType;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindProfessionAreaType();
                this.BindCardType();
             
            }
        }

        void BindCardType()
        {
            if (Request.QueryString["ot"] == "ed")
            {
                oCardType = BaseFactory.theCardType.GetModel(int.Parse(Request.QueryString["id"]));
                this.txtCardTypeName.Value = oCardType.name;
                if (oCardType.isEnable)
                {
                    this.rdIsEnable1.Checked = true;
                }
                else
                {
                    this.rdIsEnable0.Checked = true;
                }

                if (!string.IsNullOrEmpty(oCardType.photoUrl))
                {
                    this.imgLogo.Src = oCardType.photoUrl;
                    this.imgLogo.Style.Add("display", "block");
                }
                else
                {
                    this.imgLogo.Style.Add("display", "none");
                }
                if (oCardType.professionAreaTypeId != null && oCardType.professionAreaTypeId != 0)
                {
                    this.ddlProfessionAreaType.Items.FindByValue(oCardType.professionAreaTypeId.ToString()).Selected = true;
                }

                this.txtaCardTypeDescription.Value = oCardType.description;

                this.txtSortOrder.Value = oCardType.sortOrder.ToString();
            }
        }




        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (Request.QueryString["ot"] == "ed")
            {
                oCardType = BaseFactory.theCardType.GetModel(int.Parse(Request.QueryString["id"]));
                oCardType.name = this.txtCardTypeName.Value.Trim();
                oCardType.isEnable = MathLibrary.ConvertNumToBool(base.Request.Form["IsEnable"]);
                oCardType.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                oCardType.description = this.txtaCardTypeDescription.Value.Trim();
                oCardType.professionAreaTypeId = this.ddlProfessionAreaType.SelectedItem.Value == "" ? 0 : int.Parse(this.ddlProfessionAreaType.SelectedItem.Value);
                if (this.fileCardTypeLogo.PostedFile.FileName != "")
                {
                    this.oCardType.photoUrl = "/cn/logo/cardType_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileCardTypeLogo.PostedFile.FileName);
                }

                if (BaseFactory.theCardType.Update(oCardType))
                {
                    if (this.fileCardTypeLogo.PostedFile.FileName != "")
                    {
                        if (!string.IsNullOrEmpty(oCardType.photoUrl) && File.Exists(base.Server.MapPath(oCardType.photoUrl)))
                        {
                            File.Delete(base.Server.MapPath(oCardType.photoUrl));
                        }
                        this.fileCardTypeLogo.PostedFile.SaveAs(base.Server.MapPath(this.oCardType.photoUrl));
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");openNewTab('卡券类别', '/admin/goods/cardTypeList.aspx');</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                Response.Write("<script> alert('修改成功!');window.location.href='cardTypeList.aspx';</script>");
            }
            else
            {
                oCardType = new tbCardType();
                oCardType.name = this.txtCardTypeName.Value.Trim();
                oCardType.isEnable = MathLibrary.ConvertNumToBool(base.Request.Form["IsEnable"]);
                oCardType.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                oCardType.description = this.txtaCardTypeDescription.Value.Trim();
                oCardType.creater = int.Parse(base.Administrator.AdminUserId);
                oCardType.createTime = DateTime.Now;
                oCardType.professionAreaTypeId = this.ddlProfessionAreaType.SelectedItem.Value == "" ? 0 : int.Parse(this.ddlProfessionAreaType.SelectedItem.Value);
                if (this.fileCardTypeLogo.PostedFile.FileName != "")
                {
                    this.oCardType.photoUrl = "/cn/logo/cardType_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileCardTypeLogo.PostedFile.FileName);
                    this.fileCardTypeLogo.PostedFile.SaveAs(base.Server.MapPath(this.oCardType.photoUrl));
                }
                BaseFactory.theCardType.Add(oCardType);
                Response.Write("<script> alert('添加成功!');window.location.href='cardTypeList.aspx';</script>");
            }


        }

        private void BindProfessionAreaType()
        {
            DataSet dsType = new DataSet();
            DALProfessionAreaType theType = new DALProfessionAreaType();
            dsType = theType.GetList("");
            if (dsType.Tables.Count > 0)
            {
                this.ddlProfessionAreaType.DataSource = dsType.Tables[0].DefaultView;
                this.ddlProfessionAreaType.DataTextField = "Name";
                this.ddlProfessionAreaType.DataValueField = "Id";
                this.ddlProfessionAreaType.DataBind();
            }
            this.ddlProfessionAreaType.Items.Insert(0, new ListItem("所属专区", ""));


        }

    }
}
    
